package cn.szzsi.service;

import cn.szzsi.common.kit.SerialNumberUtil;
import cn.szzsi.common.model.*;
import com.jfinal.kit.StrKit;
import org.apache.xpath.operations.Or;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 订单业务类
 * Created by zhengy on 2017/3/13.
 */
public class OrderService {
    public static final OrderService service=new OrderService();

    /**
     * 订单日志(商户订单)
     * @param OrderLogType
     * @param order
     * @param info
     */
    public void saveOrderLog(Integer OrderLogType, Orders order,String info){
        OrderLog orderLog = new OrderLog();
        orderLog.set("orderLogType", OrderLogType);
        orderLog.set("orderSn",order.getStr("orderSn"));
        orderLog.set("info", info);
        orderLog.set("operator",null);
        orderLog.set("order_id",order.getStr("id"));
        orderLog.save(orderLog);
    }

    /**
     * 订单日志(平台订单)
     * @param OrderLogType
     * @param ordersPlateform
     * @param info
     */
    public void saveOrderLog(Integer OrderLogType, OrdersPlateform ordersPlateform,String info){
        OrderLog orderLog = new OrderLog();
        orderLog.set("orderLogType", OrderLogType);
        orderLog.set("orderSn",ordersPlateform.getOrderSn());
        orderLog.set("info", info);
        orderLog.set("operator",null);
        orderLog.set("order_id",ordersPlateform.getId());
        orderLog.save(orderLog);
    }


    //创建平台订单
    public OrdersPlateform createPlateFormOrder(String id,Integer totalQuantity,BigDecimal productTotalPrice,BigDecimal deliveryFee,BigDecimal paymentFee, BigDecimal paidDeposit, Integer paidPoints, BigDecimal paidPointsAsDeposit, PaymentConfig paymentConfig,Member member){
        OrdersPlateform ordersPlateform=new OrdersPlateform();
        ordersPlateform.setId(id);
        ordersPlateform.setOrderSn(SerialNumberUtil.buildOrderSn());
        ordersPlateform.setPaymentStatus(Orders.PaymentStatus.unpaid.ordinal());
        ordersPlateform.setPaymentConfigId(paymentConfig.getId());
        ordersPlateform.setPaymentConfigName(paymentConfig.getName());
        ordersPlateform.setProductTotalPrice(productTotalPrice);
        ordersPlateform.setProductTotalQuantity(totalQuantity);
        ordersPlateform.setDeliveryFee(deliveryFee);
        ordersPlateform.setPaymentFee(paymentFee);
        ordersPlateform.setPaidDeposit(paidDeposit);
        ordersPlateform.setPaidPoints(paidPoints);
        ordersPlateform.setPaidPointAsDeposit(paidPointsAsDeposit);
        ordersPlateform.setTotalAmount(productTotalPrice.add(deliveryFee).add(paymentFee));
        ordersPlateform.setMemberId(member.getId());
        ordersPlateform.setCreateDate(new Date());
        ordersPlateform.save();
        return ordersPlateform;

    }

    /**
     * //创建商户订单
     * @param deliveryType
     * @param productTotalPrice
     * @param deliveryFee
     * @param totalWeightGram
     * @param totalQuantity
     * @param receiver
     * @param memo
     * @param member
     * @param paymentConfig
     */
    public Orders createBusinessOrder(String orderId,String parentId,DeliveryType deliveryType, BigDecimal productTotalPrice, BigDecimal deliveryFee, Double totalWeightGram, Integer totalQuantity, Receiver receiver, String memo, Member member, String shopId, PaymentConfig paymentConfig){
        Orders order=new Orders();
        order.setParentId(parentId);
        order.setId(orderId);
        order.set("orderSn", SerialNumberUtil.buildOrderSn());
        order.set("orderStatus", Orders.OrderStatus.unprocessed.ordinal());
        order.set("paymentStatus", Orders.PaymentStatus.unpaid.ordinal());
        order.set("shippingStatus", Orders.ShippingStatus.unshipped.ordinal());
        order.set("deliveryTypeName",deliveryType.getName());
        order.set("paymentConfigName",paymentConfig.getName());
        order.set("productTotalPrice",productTotalPrice);
        order.set("deliveryFee",deliveryFee);//配送费用
        //order.set("paymentFee",paymentFee);//支付费用
        order.set("totalAmount",productTotalPrice.add(deliveryFee));//支付总金额
        order.set("paidAmount",new BigDecimal("0"));
        if (totalWeightGram < 1000) {
            order.set("productWeight",totalWeightGram);
            order.set("productWeightUnit", Product.WeightUnit.g.ordinal());
        } else if(totalWeightGram >= 1000 && totalWeightGram < 1000000) {
            order.set("productWeight",totalWeightGram / 1000);
            order.set("productWeightUnit", Product.WeightUnit.kg.ordinal());
        } else if(totalWeightGram >= 1000000) {
            order.set("productWeight",totalWeightGram / 1000000);
            order.set("productWeightUnit", Product.WeightUnit.t.ordinal());
        }
        order.set("productTotalQuantity",totalQuantity);
        order.set("shipName",receiver.getName());
        order.set("shipArea",receiver.getStr("areaPath"));
        order.set("shipAreaPath",receiver.getStr("areaPath"));
        order.set("shipAddress",receiver.getStr("address"));
        order.set("receiverId",receiver.getStr("id"));
        order.set("shipZipCode",receiver.getStr("zipCode"));
        order.set("shipPhone",receiver.getStr("phone"));
        order.set("shipMobile",receiver.getStr("mobile"));
        order.set("memo",memo);
        order.set("member_id",member.getId());
        order.set("uid",shopId);
        order.set("deliveryType_id",deliveryType.getId());
        order.set("paymentConfig_id",paymentConfig.getId());
        //order.setPaidDeposit(paidDeposit);
        //order.setPaidPoints(paidPoints);
        //order.setPaidPointAsDeposit(paidPointsAsDeposit);
        order.setOrderStatus(0);

        order.setCreateDate(new Date());
        order.save();
        return order;
    }


    /**
     * 用户支付完成,如果商品设置了返现金额,则给用户返现
     * @param ordersPlateform
     * @param member
     */
    public void productBackMoney(OrdersPlateform ordersPlateform,Member member){
        List<Orders> ordersList=Orders.dao.getByParentId(ordersPlateform.getId());
        for(Orders orders:ordersList){
            List<OrderItem>orderItemList=orders.getOrderItemList();
            for(OrderItem orderItem:orderItemList){
                Product product=orderItem.getProduct();
                //查看商品是否有返现
                if(product.getProductRebatePercentMoney()!=null && product.getProductRebatePercentMoney().compareTo(new BigDecimal(0))>0){
                    UserBalanceDetailService.service.savePaymentInfo(member.getId(),ordersPlateform.getTotalAmount(), UserBalanceDetail.BalanceType.backMoney.ordinal(),product.getUid(),orders.getOrderSn());//支付明细
                    UserBalanceService.service.updateBalance(member,product.getProductRebatePercentMoney());//更新会员余额结果
                }
            }
        }
    }

    /**
     * 用户支付完成,如果商品设置了返现金额,则给用户返现
     * @param orders
     * @param member
     */
    public void productBackMoney(Orders orders,Member member){
            List<OrderItem>orderItemList=orders.getOrderItemList();
            for(OrderItem orderItem:orderItemList){
                Product product=orderItem.getProduct();
                //查看商品是否有返现
                if(product.getProductRebatePercentMoney()!=null && product.getProductRebatePercentMoney().compareTo(new BigDecimal(0))>0){
                    UserBalanceDetailService.service.savePaymentInfo(member.getId(),orders.getTotalAmount(), UserBalanceDetail.BalanceType.backMoney.ordinal(),product.getUid(),orders.getOrderSn());//支付明细
                    UserBalanceService.service.updateBalance(member,product.getProductRebatePercentMoney());//更新会员余额结果
                }
            }

    }

}
